#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'

from PySide import QtGui, QtCore
from .list_group_widget import ListGroupWidget
from .pyuic.tab_view_widget import Ui_tab_view_widget

class TabViewWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(TabViewWidget, self).__init__(parent)
        self._ui = Ui_tab_view_widget()
        self._ui.setupUi(self)
        self._color = parent.skin_data['color']
        self.style = """
        *{background-color:rgba(255,255,255,150);}
        QScrollBar:vertical{background:rgba(0,0,0,0%);margin:0px,0px,0px,0px;width:11px;padding-bottom:11px;padding-top:12px;}
        QScrollBar::handle:vertical{background:rgba(0,0,0,20%);border-radius:5px;min-height:20;width:10px}
        QScrollBar::handle:vertical:hover{background:rgba(0,0,0,30%);border-radius:5px;min-height:20;}
        QScrollBar::add-line:vertical{border-image:url(:/icons/scroll_up.png);subcontrol-position:bottom;height:11px;}
        QScrollBar::add-line:vertical:hover{border-image:url(:/icons/scroll_up_hover.png);subcontrol-position:bottom;height:11px;}
        QScrollBar::sub-line:vertical{border-image:url(:/icons/scroll_down.png);subcontrol-position:top;height:11px;}
        QScrollBar::sub-line:vertical:hover{border-image:url(:/icons/scroll_down_hover.png);subcontrol-position:top;height:11px;}
        QScrollBar::add-page:vertical,QScrollBar::sub-page:vertical{background:rgba(0,0,0,10%);border-radius:5px;}

        QPushButton{border:none; height:35; min-width:40;border-bottom: 2px solid rgb(220, 220, 220);}
        """
        self.sel_style = "QPushButton{border-bottom: 2px solid rgb(%s, %s, %s);}" % (self._color.red(), self._color.green(), self._color.blue())
        self.desel_style = "QPushButton{border-bottom: 2px solid rgb(220, 220, 220);}"
        self.setStyleSheet(self.style)#'background-color:white')
        self._ui.engine_btn.setStyleSheet(self.sel_style)
        # self._ui.people_btn.setStyleSheet(style)
        # self._ui.note_btn.setStyleSheet(style)
        self.current_btn = self._ui.engine_btn
        self.current_icon = self._ui.engine_btn.icon()
        self.current_mask = ":/icons/engine_mask.png"
        self.draw_pic(self._ui.engine_btn, ":/icons/engine_mask.png")

        self._ui.engine_btn.clicked.connect(self.on_engine_btn_clicked)
        self._ui.people_btn.clicked.connect(self.on_people_btn_clicked)
        self._ui.note_btn.clicked.connect(self.on_note_btn_clicked)
        self._ui.stackedWidget.setCurrentIndex(0)

        # self.create_engine_page()
        # self.create_people_page()
        # self.create_note_page()

    def set_data(self, data):
        for group in data:
            self._create_group(group, data[group])

    def _create_group(self, group_name, data):
        list_group = ListGroupWidget(group_name=group_name, data=data)
        self._ui.engine_group_layout.addWidget(list_group)

    def create_engine_page(self):
        engine_data = self._get_engine_data()
        shotgun_group = ListGroupWidget(group_name='shotgun', data=range(10))
        offline_group = ListGroupWidget(group_name='offline', data=range(10))
        self._ui.engine_group_layout.addWidget(shotgun_group)
        self._ui.engine_group_layout.addWidget(offline_group)

    def clear_layout(self):
        while self._ui.engine_group_layout.count():
            item = self._ui.engine_group_layout.takeAt(0)
            widget =  item.widget()
            self._ui.engine_group_layout.removeWidget(widget)
            widget.close()
            widget.deleteLater()
            del widget

    def create_people_page(self):
        self._ui.people_group_layout.addWidget(QtGui.QLabel(u"敬请期待！"))

    def create_note_page(self):
        self._ui.note_group_layout.addWidget(QtGui.QLabel(u"敬请期待！"))

    def _get_engine_data(self):
        return

    def _get_people_data(self):
        return

    def on_engine_btn_clicked(self):
        if self.current_btn == self._ui.engine_btn:
            return
        last_btn = self.current_btn
        #last_btn.setIcon(self.current_icon)
        self.current_btn = self._ui.engine_btn
        self.set_btn_style(last_btn, self.current_icon)
        self.current_icon = self._ui.engine_btn.icon()
        self.current_mask = ":/icons/engine_mask.png"
        self.draw_pic(self._ui.engine_btn, self.current_mask )
        self._ui.stackedWidget.setCurrentIndex(0)

    def on_people_btn_clicked(self):
        if self.current_btn == self._ui.people_btn:
            return
        last_btn = self.current_btn
        self.current_btn = self._ui.people_btn
        self.set_btn_style(last_btn, self.current_icon)

        self.current_icon = self._ui.people_btn.icon()
        self.current_mask = ":/icons/people_mask.png"
        self.draw_pic(self._ui.people_btn, self.current_mask )
        self._ui.stackedWidget.setCurrentIndex(1)

    def on_note_btn_clicked(self):
        if self.current_btn == self._ui.note_btn:
            return
        last_btn = self.current_btn
        self.current_btn = self._ui.note_btn
        self.set_btn_style(last_btn, self.current_icon)

        self.current_icon = self._ui.note_btn.icon()
        self.current_mask = ":/icons/note_mask.png"
        self.draw_pic(self._ui.note_btn, self.current_mask)
        self._ui.stackedWidget.setCurrentIndex(2)

    def set_btn_style(self, last_btn, icon):
        last_btn.setStyleSheet(self.desel_style)
        last_btn.setIcon(icon)
        self.current_btn.setStyleSheet(self.sel_style)

    def draw_pic(self, btn, path):
        pix = QtGui.QPixmap(100,100)
        pix.fill(self._color)
        pix.setMask(QtGui.QPixmap(path))
        icon = QtGui.QIcon(pix)
        btn.setIcon(icon)

    def set_style(self, color):
        self._color = color
        self.sel_style = "QPushButton{border-bottom: 2px solid rgb(%s, %s, %s);}" % (self._color.red(), self._color.green(), self._color.blue())
        self.current_btn.setStyleSheet(self.sel_style)
        self.draw_pic(self.current_btn , self.current_mask)






